package com.ruoyi.project.stock.stock.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.bas.product.domain.BasProduct;
import com.ruoyi.project.bas.product.mapper.BasProductMapper;
import com.ruoyi.project.device.space.domain.BasCargoSpace;
import com.ruoyi.project.device.space.mapper.BasCargoSpaceMapper;
import com.ruoyi.project.port.domain.InventoryRespondData;
import com.ruoyi.project.stock.record.domain.BasStockMaintainRecord;
import com.ruoyi.project.stock.record.mapper.BasStockMaintainRecordMapper;
import com.ruoyi.project.stock.stock.domain.BasStock;
import com.ruoyi.project.stock.stock.mapper.BasStockMapper;
import com.ruoyi.project.stock.stock.service.IBasStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * 库存表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class BasStockServiceImpl implements IBasStockService
{
    @Autowired
    private BasStockMapper basStockMapper;
    @Autowired
    private BasStockMaintainRecordMapper recordMapper;
    @Autowired
    private BasProductMapper productMapper;
    @Autowired
    private BasCargoSpaceMapper basCargoSpaceMapper;

    /**
     * 查询库存表
     * 
     * @param stockId 库存表主键
     * @return 库存表
     */
    @Override
    public BasStock selectBasStockByStockId(String stockId)
    {
        return basStockMapper.selectBasStockByStockId(stockId);
    }

    /**
     * 查询库存表列表
     * 
     * @param basStock 库存表
     * @return 库存表
     */
    @Override
    public List<BasStock> selectBasStockList(BasStock basStock)
    {
        return basStockMapper.selectBasStockList(basStock);
    }

    /**
     * 库存查询接口
     * @param basStock
     * @return
     */
    @Override
    public List<InventoryRespondData> selectBasStockListForApi(BasStock basStock) {
        return basStockMapper.selectBasStockListForApi(basStock);
    }

    /**
     * 新增库存表
     * 
     * @param basStock 库存表
     * @return 结果
     */
    @Override
    public int insertBasStock(HttpServletRequest request,BasStock basStock, String stockId)
    {
        int countStock = basStockMapper.selectCountStockByCode(basStock.getTrayCode(), basStock.getCargoSpaceId());
        if (countStock > 0){
            return 1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        basStock.setStockId(stockId);
        basStock.setInDate(sdf.format(new Date()));
        basStock.setBackDate(sdf1.format(new Date()));
//        basStock.setLotNo(sdf.format(new Date()));
        BasProduct basProduct = productMapper.selectBasProductByProductId(basStock.getProductId());
        if (basProduct != null){
            basStock.setLineId(basProduct.getProductName());
            basStock.setProductGuid(basProduct.getMainWarehouse());
            basStock.setProductDetailId(basProduct.getProportions());
            //缺少垛型
            basStock.setStackingId("1");
        }
        basStock.setJobId("0");
        basStock.setStockStatus("0");
//        basStock.setProductStatus("0");
        basStock.setOutQuantity(BigDecimal.ZERO);
        basStock.setInType("1");
//        basStock.setInClassId("0");
//        basStock.setWarehouseId();
        addStockMaintainRecord("新增",basStock);
        BasCargoSpace basCargoSpace = new BasCargoSpace();
        basCargoSpace.setStatus("2");
        basCargoSpace.setCargoSpaceId(basStock.getCargoSpaceId());
        basCargoSpaceMapper.updateBasCargoSpace(basCargoSpace);
        basStockMapper.insertBasStock(basStock);
        Integer sumQuantity = basStockMapper.selectSumQuantity(basStock.getTrayCode(), basStock.getCargoSpaceId());
        basStockMapper.updateStockByIdAndCode(basStock.getCargoSpaceId(), basStock.getTrayCode(), String.valueOf(sumQuantity));
        return 2;
    }

    /**
     * 修改库存表
     * 
     * @param basStock 库存表
     * @return 结果
     */
    @Override
    public int updateBasStock(BasStock basStock)
    {
        addStockMaintainRecord("修改", basStock);
        return basStockMapper.updateBasStock(basStock);
    }

    /**
     * 批量删除库存表
     * 
     * @param stockIds 需要删除的库存表主键
     * @return 结果
     */
    @Override
    public int deleteBasStockByStockIds(String stockIds)
    {
        String[] split = stockIds.split(",");
        for(String stockId:split){
            BasStock basStock = basStockMapper.selectBasStockByStockId(stockId);
            basStock.setQuantity(BigDecimal.ZERO);
            addStockMaintainRecord("删除",basStock);
        }

        return basStockMapper.deleteBasStockByStockIds(Convert.toStrArray(stockIds));
    }

    /**
     * 删除库存表信息
     * 
     * @param stockId 库存表主键
     * @return 结果
     */
    @Override
    public int deleteBasStockByStockId(String stockId)
    {
        return basStockMapper.deleteBasStockByStockId(stockId);
    }

    @Override
    public String assignCargoOut(String trayCode,String exitId) {
        String msg="";
        HashMap<String, String> map = new HashMap<>();
        map.put("v_traycode",trayCode);
        map.put("v_exit_id",exitId);
        basStockMapper.assignCargoOut(map);
        String result=map.get("v_result");
        if("1".equals(result)){
            msg="该托盘无库存";
        }else if("2".equals(result)){
            msg="该托盘已有作业";
        }else if("3".equals(result)){
            msg="出口选择错误";
        }else if("9".equals(result)){
            msg="存储过程异常";
        }else{
            msg="操作成功";
        }
        return msg;
    }

    /**
* 增加库存维护记录
*/
    public void addStockMaintainRecord(String method,BasStock basStock) {
        BasStock basStock1 = basStockMapper.selectBasStockByStockId(basStock.getStockId());
        BasStockMaintainRecord maintainRecord= new BasStockMaintainRecord();
        maintainRecord.setWhGuid(UUID.randomUUID().toString());
        maintainRecord.setStockId(basStock.getStockId());
        maintainRecord.setCargoSpaceId(basStock.getCargoSpaceId());
        maintainRecord.setTrayCode(basStock.getTrayCode());
        maintainRecord.setProductId(basStock.getProductId());
        maintainRecord.setProductName(basStock.getLineId());
        maintainRecord.setLotNo(basStock.getLotNo());
        SysUser user = (SysUser) ServletUtils.getSubject().getPrincipal();
        maintainRecord.setPrintDate(user.getUserName());
        if (basStock1 != null){
            maintainRecord.setOldQuantity(String.valueOf(basStock1.getQuantity()));
        }else {
            maintainRecord.setOldQuantity("0");
        }

        maintainRecord.setQuantity(String.valueOf(basStock.getQuantity()));
        maintainRecord.setMethod(method);
        maintainRecord.setWarehouseId(basStock.getWarehouseId());
        maintainRecord.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        recordMapper.insertBasStockMaintainRecord(maintainRecord);
    }

    @Override
    public List<BasStock> selectErrorStockList(BasStock basStock) {
      return   basStockMapper.selectErrorStockList(basStock);

    }
}
